package ExceptionDemo;

import java.util.Scanner;

public class StrLenExcep {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String str=sc.next();

        System.out.println("使用方法length的长度为："+str.length());

        test(str);

    }

    private static void test(String str)
    {
        int index=0;
        try {
            while(true)
            {
                str.charAt(index);
                index++;
            }
        } catch (Exception e) {
            System.out.println("此字符串的长度："+index);
        }
    }
}
